package model;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.bind.annotation.XmlElement;

import controller.ID3Parser;
import controller.ID3Writer;
import controller.InvalidID3TagException;

/**
 * The MP3FileMutableTreeNode is a subclass of the DefaultMutableTreeNode
 * and represents a MP3File in the TreeModel.
 * 
 *
 */

public class MP3FileMutableTreeNode extends DefaultMutableTreeNode {

	private static final long serialVersionUID = -2594989601274238899L;
	private MP3File mp3File;
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}




	@XmlElement(required=true)
	public MP3File getMP3File() {
		return mp3File;
		
	}
	public void setMP3File(MP3File mp3File) {
		this.mp3File = mp3File;
	}

	//constructor for a treeNode with informations about a particular mp3 file
	public MP3FileMutableTreeNode(String pathToFile, String title) {
		super(title);
		if(!pathToFile.equals("")) {
			Path path = Paths.get(pathToFile);
		
			try {
				mp3File = ID3Parser.parseMP3File(path);
			} catch (IOException | InvalidID3TagException e) {
				e.printStackTrace();
			}
//			mp3File.setFilePath(path);
		}
		else {
			mp3File = null;
		}
	}	
	
	//constructor for a treeNode with a MP3file as parameter
	public MP3FileMutableTreeNode(MP3File mp3) {
		super(mp3.getTitle());
		mp3File = mp3;
	}
		
	public String getMP3Title() {
		return mp3File.getTitle();
	}

	//default constructor
	public MP3FileMutableTreeNode() {
		mp3File = null;
	}
	/*
	 * methods to change data with the input from the gui
	 */
	public void changeAlbumOfMP3File(String album){
		ID3Writer.changeContentMP3FrameWithKey(this.mp3File, "TALB", album);
		this.mp3File.setAlbum(album);
	}

	public void changeArtistOfMP3File(String artist){
		ID3Writer.changeContentMP3FrameWithKey(this.mp3File, "TPE1", artist);
		this.mp3File.setArtist(artist);
	}
	
	public void changeTitleOfMP3File(String title){
		ID3Writer.changeContentMP3FrameWithKey(this.mp3File, "TIT2", title);
		this.mp3File.setTitle(title);
		this.setUserObject(title);
	}
	
	public void changeYearOfMP3File(Integer year){
		ID3Writer.changeContentMP3FrameWithKey(this.mp3File, "TYER", year.toString());
		this.mp3File.setYear(year);
	}
	
	public void changeCoverofMP3File(Image image) {
		ImageIcon icon = new ImageIcon(image);
		int height = icon.getIconHeight();
		int width = icon.getIconWidth();

		BufferedImage im = new BufferedImage(width, height,BufferedImage.TYPE_4BYTE_ABGR);
		im.getGraphics().drawImage(image, 0,0, icon.getImageObserver());
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
	 	try {
		 	ImageIO.write(im, "png", stream);
		}catch (IOException e){
			e.printStackTrace();
	 	}
		byte[] imageData = stream.toByteArray();
		try {
			stream.close();
		} catch (IOException e) {
			System.out.println("outputstream konnte nicht geschlossen werden");
			e.printStackTrace();
		}
		ID3Writer.changeContentMP3FrameWithKey(this.mp3File, "APIC", imageData);
		
		this.mp3File.setCoverImage(image);
	}

	//TODO more to add..
}
